from flask import jsonify, send_from_directory
from services.detection_service import detection_service
import os

def run_web_detection():
    """运行Web缺陷识别"""
    try:
        result = detection_service.run_web_detection()
        return jsonify(result)
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'Web缺陷识别执行失败: {str(e)}'
        }), 500

def run_mysql_detection():
    """运行MySQL缺陷识别"""
    try:
        result = detection_service.run_mysql_detection()
        return jsonify(result)
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'MySQL缺陷识别执行失败: {str(e)}'
        }), 500

def list_mysql_reports():
    """获取MySQL报告列表"""
    try:
        result = detection_service.get_mysql_reports()
        return jsonify(result)
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'获取MySQL报告列表失败: {str(e)}',
            'files': []
        }), 500

def list_web_reports():
    """获取Web报告列表"""
    try:
        result = detection_service.get_web_reports()
        return jsonify(result)
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'获取Web报告列表失败: {str(e)}',
            'files': []
        }), 500

def get_mysql_report_content(filename):
    """获取MySQL报告内容"""
    try:
        # 获取报告内容
        result = detection_service.get_mysql_report_content(filename)
        if result["success"]:
            return jsonify(result)
        else:
            return jsonify(result), 404
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'获取MySQL报告内容失败: {str(e)}'
        }), 500

def get_web_report_content(filename):
    """获取Web报告内容"""
    try:
        # 获取报告内容
        result = detection_service.get_web_report_content(filename)
        if result["success"]:
            return jsonify(result)
        else:
            return jsonify(result), 404
    except Exception as e:
        return jsonify({
            'success': False,
            'message': f'获取Web报告内容失败: {str(e)}'
        }), 500